package com.px.auditRecord.service.impl;

import java.util.List;

import com.px.auditRecord.domain.RoutesSurface;
import com.px.auditRecord.domain.RoutesSurfacePoint;
import com.px.auditRecord.mapper.RoutesSurfaceMapper;
import com.px.auditRecord.mapper.RoutesSurfacePointMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.px.auditRecord.mapper.RoutesBasicMapper;
import com.px.auditRecord.domain.RoutesBasic;
import com.px.auditRecord.service.IRoutesBasicService;

/**
 * hangxianService业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class RoutesBasicServiceImpl implements IRoutesBasicService 
{
    @Autowired
    private RoutesBasicMapper routesBasicMapper;

    @Autowired
    private RoutesSurfaceMapper routesSurfaceMapper;

    @Autowired
    private RoutesSurfacePointMapper routesSurfacePointMapper;
    /**
     * 查询hangxian
     * 
     * @param id hangxian主键
     * @return hangxian
     */
    @Override
    public RoutesBasic selectRoutesBasicById(Integer id)
    {
        RoutesBasic routesBasic = routesBasicMapper.selectRoutesBasicById(id);

        List<RoutesSurface> routesSurfaceList = routesSurfaceMapper.selectRoutesSurfaceByBasicId(id);
        routesSurfaceList.forEach(routesSurface -> {
            List<RoutesSurfacePoint> routesSurfacePointList = routesSurfacePointMapper.selectRoutesSurfacePointListByRoutesSurfaceId(routesSurface.getId());
            routesSurface.setRoutesSurfacePointList(routesSurfacePointList);
        });
        routesBasic.setRoutesSurfaceList(routesSurfaceList);
        return routesBasic;
    }

    /**
     * 查询hangxian列表
     * 
     * @param routesBasic hangxian
     * @return hangxian
     */
    @Override
    public List<RoutesBasic> selectRoutesBasicList(RoutesBasic routesBasic)
    {
        return routesBasicMapper.selectRoutesBasicList(routesBasic);
    }

    /**
     * 新增hangxian
     * 
     * @param routesBasic hangxian
     * @return 结果
     */
    @Override
    public int insertRoutesBasic(RoutesBasic routesBasic)
    {
        int count = routesBasicMapper.insertRoutesBasic(routesBasic);
        routesBasic.getRoutesSurfaceList().forEach(routesSurface ->{
            routesSurface.setBasicId(routesBasic.getId());
            int i = 0;
            routesSurfaceMapper.insertRoutesSurface(routesSurface);
            routesSurface.getRoutesSurfacePointList().forEach(routesSurfacePoint ->{
                routesSurfacePoint.setRoutesSurfaceId(routesSurface.getId());
                routesSurfacePoint.setPointIndex(i);
                routesSurfacePointMapper.insertRoutesSurfacePoint(routesSurfacePoint);
            });
        });
        return count;
    }

    /**
     * 修改hangxian
     * 
     * @param routesBasic hangxian
     * @return 结果
     */
    @Override
    public int updateRoutesBasic(RoutesBasic routesBasic)
    {
        int count = routesBasicMapper.updateRoutesBasic(routesBasic);
        //删除面
        routesSurfaceMapper.deleteRoutesSurfaceByBasicId(routesBasic.getId());
        routesBasic.getRoutesSurfaceList().forEach(routesSurface ->{
            routesSurface.setBasicId(routesBasic.getId());
            int i = 0;
            //删除点
            routesSurfacePointMapper.deleteRoutesSurfacePointByRoutesSurfaceId(routesSurface.getId());
            routesSurfaceMapper.insertRoutesSurface(routesSurface);
            routesSurface.getRoutesSurfacePointList().forEach(routesSurfacePoint ->{
                routesSurfacePoint.setRoutesSurfaceId(routesSurface.getId());
                routesSurfacePoint.setPointIndex(i);
                routesSurfacePointMapper.insertRoutesSurfacePoint(routesSurfacePoint);
            });
        });
        return count;
    }

    /**
     * 批量删除hangxian
     * 
     * @param ids 需要删除的hangxian主键
     * @return 结果
     */
    @Override
    public void deleteRoutesBasicByIds(Integer[] ids)
    {
        for (Integer id : ids) {
            List<RoutesSurface> routesSurfaceList = routesSurfaceMapper.selectRoutesSurfaceByBasicId(id);
            routesSurfaceList.forEach(routesSurface -> {
                routesSurfacePointMapper.deleteRoutesSurfacePointByRoutesSurfaceId(routesSurface.getId());
            });
            routesSurfaceMapper.deleteRoutesSurfaceByBasicId(id);
            routesBasicMapper.deleteRoutesBasicById(id);

        }
    }

    /**
     * 删除hangxian信息
     * 
     * @param id hangxian主键
     * @return 结果
     */
    @Override
    public int deleteRoutesBasicById(Integer id)
    {
        return routesBasicMapper.deleteRoutesBasicById(id);
    }
}
